package com.claimSettlement.controller;

import com.claimSettlement.pojo.*;
import com.claimSettlement.service.ClaimsService;
import com.claimSettlement.service.SurveyLossOpenFeginService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/*
 *菜单控制层
 *宁杰
 */
@RestController
@RequestMapping("/claims")
@CrossOrigin
public class ClaimsController {
    @Autowired
    ClaimsService claimsService;
    @Autowired
    SurveyLossOpenFeginService surveyLossOpenFeginService;

    @RequestMapping("/getSettlement")
    public PageInfo<Settlement> getSettlement(Integer userId, Integer page) {
        PageHelper.startPage(page,4);
        List<Settlement> list = claimsService.getSettlement(userId);
        PageInfo<Settlement> pageInfo = new PageInfo<Settlement>(list);
        //一共pageInfo.pages页 --当前页是pageInfo.pageNum  -- 共有pageInfo.total 条数据
        return pageInfo;
    }
    @RequestMapping("/insertClaims")
    public Integer insertClaims(@RequestBody Map<String,Object> map) {
        return claimsService.insertClaims(map);
    }
    @RequestMapping("/updateClaims")
    public Integer updateClaims(@RequestBody Map<String,Object> map) {
        return claimsService.updateClaims(map);
    }
    @RequestMapping("/deleteClaims")
    public Integer deleteClaims(@RequestBody Settlement settlement) {
        return claimsService.deleteClaims(settlement.getSettlementNumber());
    }
    @RequestMapping("/flowState")
    public Integer flowState(Integer flowId) {
        return claimsService.flowState(flowId);
    }
    @RequestMapping("/claim_check")
    public Integer claim_check(Integer settlement_number) {
        return claimsService.claim_check(settlement_number);
    }
    @RequestMapping("/updateClaimCheck")
    public Integer updateClaimCheck(@RequestBody Settlement settlement) {
        System.out.println(settlement);
        Integer claim_check=claimsService.claim_check(settlement.getSettlementNumber());//获取理赔当前状态
        if(settlement.getClaimCheck()==1) {
            if (claim_check == 0 || claim_check == 2||claim_check==3) {
                claimsService.updateClaimCheck(settlement.getSettlementNumber(), 1);
                claimsService.updateCaseState(settlement.getSettlementNumber());
                claimsService.updateCloseCaseTime(settlement.getSettlementNumber());
                return 1;
            }
            return 0;
        }else{
            if (claim_check ==0) {
                claimsService.updateClaimCheck(settlement.getSettlementNumber(), 2);
                return 1;
            }else if(claim_check==2){
                System.out.println("dsf");
                claimsService.updateClaimCheck(settlement.getSettlementNumber(), 3);
                //修改核损状态为暂存
                Map<String,Object> map=new HashMap<String, Object>();
                map.put("caseId",settlement.getCaseId());
                map.put("state",3);
                surveyLossOpenFeginService.updateLossState(map);
                return 1;
            }
            return 0;
        }
    }
    @RequestMapping("/updateFlowState")
    public Integer updateFlowState(@RequestBody Map<String,Integer> map) {
        return claimsService.updateFlowState(map.get("flowId"));
    }
    @RequestMapping("/getSettlementCond")
    public PageInfo<Settlement> getSettlementCond(Integer userId,String settlement_number, String warrantyId,Integer page) {
        PageHelper.startPage(page,4);
        List<Settlement> list = claimsService.getSettlementCond(userId,settlement_number,warrantyId);
        PageInfo<Settlement> pageInfo = new PageInfo<Settlement>(list);
        //一共pageInfo.pages页 --当前页是pageInfo.pageNum  -- 共有pageInfo.total 条数据
        return pageInfo;
    }
    @RequestMapping("/updateCaseState")
    public Integer updateCaseState(Integer caseId) {
        return claimsService.updateCaseState(caseId);
    }
    @RequestMapping("/updateCloseCaseTime")
    public Integer updateCloseCaseTime(Integer caseId) {
        return claimsService.updateCloseCaseTime(caseId);
    }
    @RequestMapping("/settlementMap")
    public Map<String,Object> settlementMap(Integer lossId,Integer settlement_number,Integer option,Integer userId) {
        Map<String,Object> map=new HashMap<String, Object>();
        map.put("lossDetermination",claimsService.getSurveyLossInfo(lossId));
        if(option==2){
            map.put("adjustmentInfos",claimsService.getAdjustment(userId));
            System.out.println(userId);
            System.out.println(claimsService.getAdjustment(userId));
            map.put("settlementObject",claimsService.getSettlementId(settlement_number));
        }else if(option==1){
            map.put("adjustmentInfos",claimsService.getAdjustmentInfo(lossId));
        }
        return map;
    }
    @RequestMapping("/getRiskTypeAll")
    public List<RiskType> getRiskTypeAll(){
        return claimsService.getRiskTypeAll();
    }
    @RequestMapping("/getSettlementTask")
    public List<LossDetermination> getSettlementTask(Integer userId) {
        return claimsService.getSettlementTask(userId);
    }
}
